import { clearDraft } from '@/utils/draft';
import { history, useMutation, useQuery, useQueryClient } from '@umijs/max';
import { notification } from 'antd';
import { request } from '../axios';
import QUERY_KEYS from '../constant';
import APP_PRE from '../constant/app-pre';

const prefix = `${APP_PRE.APP_NAME}/article`;

const useGetAllArticles = (accountId: number) => {
  return useQuery([QUERY_KEYS.ARTICLES_GET_ALL, accountId], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/getAll`, {
      method: 'GET',
      params: {
        accountId,
      },
    }),
  );
};

const useGetAllNews = () => {
  return useQuery([QUERY_KEYS.ARTICLES_NEWS_GET_ALL], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getAllNews`, {
      method: 'GET',
    }),
  );
};

const useGetTopNArticle = (num: number) => {
  return useQuery([QUERY_KEYS.ARTICLES_TOP_N_GET_ALL], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getTopNArticle`, {
      method: 'GET',
      params: { num },
    }),
  );
};

const useGetAbout = (type: number) => {
  return useQuery([QUERY_KEYS.ARTICLES_AOUNT_BY_TYPE, type], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getAbout`, {
      method: 'GET',
      params: {
        type,
      },
    }),
  );
};

const useGetTopN = (num: number) => {
  return useQuery([QUERY_KEYS.ARTICLES_Top_ARTICLE_BY_TIME, num], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getTopN`, {
      method: 'GET',
      params: {
        num,
      },
    }),
  );
};

const useGetTopNNEWS = (num: number) => {
  return useQuery([QUERY_KEYS.ARTICLES_Top_NEWS_BY_TIME, num], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getTopNNEWS`, {
      method: 'GET',
      params: {
        num,
      },
    }),
  );
};

const useGetAllQuestion = () => {
  return useQuery([QUERY_KEYS.ARTICLES_QUESTION_GET_ALL], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/getAllQuestion`, {
      method: 'GET',
    }),
  );
};

const useAddArticle = (accountId: number, type: number = 0) => {
  const queryClient = useQueryClient();
  return useMutation(
    (article: API.IArticles) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/add`, {
        method: 'POST',
        data: {
          accountId,
          ...article,
        },
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          // 发布文章
          if (type === 0) {
            notification.success({
              message: '发布文章成功',
            });
            queryClient.invalidateQueries([QUERY_KEYS.ARTICLES_GET_ALL]);
            clearDraft();
            history.push('/dashboard/my-articles');
          } else if (type === 1) {
            // 提问
            notification.success({
              message: '提问成功',
            });
            queryClient.invalidateQueries([
              QUERY_KEYS.ARTICLES_QUESTION_GET_ALL,
            ]);
          }
        }
      },
    },
  );
};

const useUpdateArticle = () => {
  const queryCliet = useQueryClient();
  return useMutation(
    (article: API.IArticles) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/update`, {
        method: 'POST',
        data: article,
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          notification.success({
            message: '修改文章成功',
          });
          queryCliet.invalidateQueries([QUERY_KEYS.ARTICLES_GET_ALL]);
          clearDraft();
          history.push('/dashboard/my-articles');
        }
      },
    },
  );
};

const useDeleteArticle = () => {
  const queryCliet = useQueryClient();
  return useMutation(
    (id: number) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/delete`, {
        method: 'POST',
        params: { id },
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          notification.success({
            message: '删除文章成功',
          });
          queryCliet.invalidateQueries([QUERY_KEYS.ARTICLES_GET_ALL]);
        }
      },
    },
  );
};

const useGetOneArticle = (id: number, enabled: boolean = true) => {
  return useQuery(
    [QUERY_KEYS.ARTICLES_GET_ONE, id, enabled],
    () =>
      request<API.IResult<API.IArticles & API.IUserInfo>>(
        `${prefix}/public/getOne`,
        {
          method: 'GET',
          params: {
            id,
          },
        },
      ),
    {
      enabled,
    },
  );
};

const useSearchArticle = (key: string) => {
  return useQuery([QUERY_KEYS.ARTICLES_SEARCH, key], () =>
    request<API.IResult<API.IArticles[]>>(`${prefix}/public/search`, {
      method: 'GET',
      params: {
        key,
      },
    }),
  );
};

export {
  useGetAllArticles,
  useAddArticle,
  useGetOneArticle,
  useUpdateArticle,
  useDeleteArticle,
  useSearchArticle,
  useGetAllQuestion,
  useGetAllNews,
  useGetTopN,
  useGetAbout,
  useGetTopNNEWS,
  useGetTopNArticle,
};
